const { sequelize } = require("../db/index")
const { DataTypes, Model } = require("sequelize")
const User = require("./user_model")
const Cleaner = require("./cleaner_model")
const moment = require("moment")

class Customer extends Model {
  // static async hasCate(id) {
  //   const cate = await Cate.findOne({ where: { id } })
  //   if (cate === null) {
  //     return false
  //   } else {
  //     return true
  //   }
  // }
  // static async createCustomer({
  //   store_id,
  //   company_name,
  //   full_company_name,
  //   customer_name,
  //   phone,
  //   tel_phone,
  //   area,
  //   area_address,
  //   company_type,
  //   customer_type,
  //   bank_name,
  //   bank,
  //   status,
  //   remarks,
  //   is_delete
  // }) {
  //   const goods = await Customer.create({
  //     store_id,
  //     company_name,
  //     full_company_name,
  //     customer_name,
  //     phone,
  //     tel_phone,
  //     area,
  //     area_address,
  //     company_type,
  //     customer_type,
  //     bank_name,
  //     bank,
  //     status,
  //     remarks,
  //     is_delete
  //   })
  //   return goods
  // }
  // static async createCustomer({
  //   store_id,
  //   company_name,
  //   full_company_name,
  //   customer_name,
  //   phone,
  //   tel_phone,
  //   area,
  //   area_address,
  //   company_type,
  //   customer_type,
  //   bank_name,
  //   bank,
  //   status,
  //   remarks,
  //   is_delete
  // }) {
  //   const goods = await Customer.findOrCreate({
  //     where: { store_id, full_company_name },
  //     default: {
  //       store_id,
  //       company_name,
  //       full_company_name,
  //       customer_name,
  //       phone,
  //       tel_phone,
  //       area,
  //       area_address,
  //       company_type,
  //       customer_type,
  //       bank_name,
  //       bank,
  //       status,
  //       remarks,
  //       is_delete
  //     }
  //   })
  //   // const goods = await Customer.create({ goods_name, full_goods_name, cat_id, attr, stock, sales, weight, desc, ord, store_id, is_hot, is_recommend, status, is_delete, slider_image, content })
  //   return goods
  // }
}

Customer.init(
  {
    id: {
      comment: "客户ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    store_id: {
      comment: "门店ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false
    },
    company_name: {
      comment: "单位名称",
      type: DataTypes.STRING,
      allowNull: false
    },
    full_company_name: {
      comment: "单位名称—简称",
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    customer_name: {
      comment: "联系人",
      type: DataTypes.STRING,
      allowNull: false
    },
    phone: {
      comment: "移动电话",
      type: DataTypes.STRING,
      allowNull: false
    },
    tel_phone: {
      comment: "固定电话",
      type: DataTypes.STRING,
      allowNull: false
    },
    area: {
      comment: "区域",
      type: DataTypes.STRING,
      allowNull: false
    },
    area_address: {
      comment: "详细地址",
      type: DataTypes.STRING,
      allowNull: false
    },
    company_type: {
      comment: "企业类型",
      type: DataTypes.STRING,
      allowNull: false
    },
    customer_type: {
      comment: "供销类型",
      type: DataTypes.STRING,
      allowNull: false
    },
    bank_name: {
      comment: "开户行",
      type: DataTypes.STRING,
      allowNull: false
    },
    bank: {
      comment: "银行帐号",
      type: DataTypes.STRING,
      allowNull: false
    },
    status: {
      comment: "状态",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    remarks: {
      comment: "备注",
      type: DataTypes.STRING,
      allowNull: true
    },
    is_delete: {
      comment: "是否删除",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    config: {
      comment: "其他数据",
      type: DataTypes.STRING,
      allowNull: true
    },
    create_time: {
      comment: "创建时间",
      type: DataTypes.DATE,
      get() {
        return moment(this.getDataValue("update_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    },
    update_time: {
      comment: "更新时间",
      type: DataTypes.STRING,
      get() {
        return moment(this.getDataValue("update_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    }
  },
  {
    sequelize,
    modelName: "sp_customer",
    freezeTableName: true,
    paranoid: false,
    timestamps: false
  }
)

module.exports = Customer
